const cron = require('node-cron');
const { default: axios } = require("axios");

const braceletMiddleware = require('./neusoft_bracelet');

// 护理计划定时任务
// cron.schedule('*/5 * * * * *', huliPlan);
cron.schedule('*/5 * * * *', huliPlan);

// //  报警计划定时任务
// // cron.schedule('*/1 * * * *', huliPlan);

// 护理计划url
const huliPlanUrl = 'http://10.10.76.67:50001/api/ylsystem/careplanfivemin';

async function huliPlan() {
  try {
    let re = await axios.get(huliPlanUrl);
    if (re.data && re.data.code == 100000) {
      // console.log('re.data', re.data);
      // console.log('re.data.data', re.data.data);
      // let body =
      //       '{"code":100000,"data":[{"id":42,"elder_id":10,"item_name":"16：00鼻饲","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 20","building_id":1,"room_id":22,"bed_id":12,"buildingName":"D6","roomNo":"6211","bedNo":"1","elderName":"王淑清"},{"id":43,"elder_id":10,"item_name":"16：00翻身扣背","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 10","building_id":1,"room_id":22,"bed_id":12,"buildingName":"D6","roomNo":"6211","bedNo":"1","elderName":"王淑清"},{"id":71,"elder_id":17,"item_name":"16：00鼻饲","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 20","building_id":1,"room_id":20,"bed_id":11,"buildingName":"D6","roomNo":"6209","bedNo":"1","elderName":"孙德水"},{"id":72,"elder_id":17,"item_name":"16：00翻身扣背","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 10","building_id":1,"room_id":20,"bed_id":11,"buildingName":"D6","roomNo":"6209","bedNo":"1","elderName":"孙德水"},{"id":100,"elder_id":17,"item_name":"16：00鼻饲","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 20","building_id":1,"room_id":20,"bed_id":11,"buildingName":"D6","roomNo":"6209","bedNo":"1","elderName":"孙德水"},{"id":101,"elder_id":17,"item_name":"16：00翻身扣背","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 10","building_id":1,"room_id":20,"bed_id":11,"buildingName":"D6","roomNo":"6209","bedNo":"1","elderName":"孙德水"},{"id":131,"elder_id":17,"item_name":"16：00鼻饲","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 20","building_id":1,"room_id":20,"bed_id":11,"buildingName":"D6","roomNo":"6209","bedNo":"1","elderName":"孙德水"},{"id":132,"elder_id":17,"item_name":"16：00翻身扣背","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 10","building_id":1,"room_id":20,"bed_id":11,"buildingName":"D6","roomNo":"6209","bedNo":"1","elderName":"孙德水"},{"id":320,"elder_id":22,"item_name":"16：00鼻饲","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 20","building_id":1,"room_id":25,"bed_id":34,"buildingName":"D6","roomNo":"6215","bedNo":"2","elderName":"韩作云"},{"id":321,"elder_id":22,"item_name":"16：00翻身扣背","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 10","building_id":1,"room_id":25,"bed_id":34,"buildingName":"D6","roomNo":"6215","bedNo":"2","elderName":"韩作云"},{"id":348,"elder_id":22,"item_name":"翻身","day_value":null,"week_value":null,"time_start":"16 : 00","time_end":"16 : 05","building_id":1,"room_id":25,"bed_id":34,"buildingName":"D6","roomNo":"6215","bedNo":"2","elderName":"韩作云"}],"msg":""}';
          
      // re.data = JSON.parse(body);
      console.log('护理计划列表:', re.data.data.length);
      let smsList = re.data.data;
      for (let item of smsList) {
        let obj = {
          title: '护理通知',
          content:
            '姓名：【' +
            item['elderName'] +
            '】 护理项目：【' +
            item['item_name'] +
            '】 时间：【' +
            item['time_start'] +
            '-' +
            item['time_end'] +
            '】 位置：【' +
            item['buildingName'] +
            ' ' +
            item['roomNo'] +
            ' ' +
            item['bedNo'] +
            '床】',
        };
        sendHuliSms(obj);
      }
    }
  } catch (error) {
    console.log('huliPlan corn Error:', error);
  }
}

/**
 * 发送消息
 * @param {*} data
 */
async function sendHuliSms(data) {
  let obj = {
    title: data.title,
    content: data.content,
    dcIds: data.dcIds,
  };
  try {
    let kfkcObj = await braceletMiddleware.pushAcMessage(obj);
    if (kfkcObj.code == 1 || kfkcObj.msg == "success") {
        console.log('发送护理计划：' + data.content + " 成功");
    }
  } catch (error) {
    console.log('发送护理计划 Error：', error);
  }
}
